package exam.test3;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

public class Playoffs {

    private ArrayList<FootballTeam> footballTeams;
    private FootballTeam winner;

    public Playoffs() {
        this.footballTeams = new ArrayList<>(16);
        for (int i = 0; i < 16; i++) {
            this.footballTeams.add(new FootballTeam("Team-" + (i + 1)));
        }
    }

    public void tournamentStart() {
        Random random = new Random();
        ArrayList<FootballTeam> footballTeamsTmp = new ArrayList<>(footballTeams);
        List<FootballTeam> lossTeams = new ArrayList<>();
        while (footballTeamsTmp.size() > 1) {
            lossTeams.clear();
            Collections.shuffle(footballTeamsTmp);
            for (int i = 0; i + 1 < footballTeamsTmp.size(); i += 2) {
                FootballTeam teamOne = footballTeamsTmp.get(i);
                FootballTeam teamTwo = footballTeamsTmp.get(i + 1);
                if (random.nextBoolean()) {
                    lossTeams.add(teamOne);
                } else {
                    lossTeams.add(teamTwo);
                }
            }
            footballTeamsTmp.removeAll(lossTeams);
        }

        winner = footballTeamsTmp.get(0);

        System.out.println("The winner of Super Bowl is " + winner.getTeamName());
    }

    public static void main(String[] args) {
        Playoffs playoffs = new Playoffs();
        playoffs.tournamentStart();
    }
}
